package com.lczyfz.istep.common.utils;

/**
 * Created by CQH on 2017/4/11.
 */

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class XmlUtils {
    public static void main(String[] args) throws Exception {
        String str = "<?xml version='1.0' encoding='UTF-8'?><modes><mode classname=\"module\" class=\"module\" attr_prop_飞哥=\"27\" attr_prop_attri2=\"3\" attr_prop_attri3=\"4\" attr_prop_attri4=\"8\" id=\"12\" title=\"\" backImgSrc=\"http://localhost:8080/static/images/topology/switch-istep.png\" top=\"133\" left=\"246\" zIndex=\"12\" width=\"50\" height=\"50\"/>\n" +
                "<mode classname=\"module\" class=\"module\" attr_prop_飞哥=\"27\" attr_prop_attri2=\"3\" attr_prop_attri3=\"4\" attr_prop_attri4=\"8\" id=\"12\" title=\"\" backImgSrc=\"http://localhost:8080/static/images/topology/switch-istep.png\" top=\"133\" left=\"246\" zIndex=\"12\" width=\"50\" height=\"50\"/>\n" +
                "</modes>";
        String str1="<?xml version=\"1.0\" encoding=\"utf8\"?><modes><mode classname=\"module\" class=\"module\" attr_prop_VmTemplate=\"1=f4de87a7-d1c4-45d9-8c37-dfa9797bc6c3&amp;&amp;vm424,0=a97a265d-4659-4db6-8fa5-e13804a55c4f&amp;&amp;vm11h,0=0bd53497-da21-4774-9652-1ea332f8d371&amp;&amp;vm1h,0=da6224230a784c7ba0b123c2399678cf&amp;&amp;vm2d,0=608426aac94d43eda4a98cde4b772029&amp;&amp;vm2d,0=157d21583f584664b1781b0431891517&amp;&amp;vm2d,0=208b22ab3e97438b83fca2424a0ad02d&amp;&amp;vm2\" id=\"20\" title=\"\" backImgSrc=\"http://localhost:8080/static/images/topology/baseMode4.png\" top=\"300\" left=\"626\" zIndex=\"20\" width=\"50\" height=\"50\"/><mode classname=\"module\" class=\"module\" attr_prop_VmTemplate=\"1=f4de87a7-d1c4-45d9-8c37-dfa9797bc6c3&amp;&amp;vm424,0=a97a265d-4659-4db6-8fa5-e13804a55c4f&amp;&amp;vm11h,0=0bd53497-da21-4774-9652-1ea332f8d371&amp;&amp;vm1h,0=da6224230a784c7ba0b123c2399678cf&amp;&amp;vm2d,0=608426aac94d43eda4a98cde4b772029&amp;&amp;vm2d,0=157d21583f584664b1781b0431891517&amp;&amp;vm2d,0=208b22ab3e97438b83...1f5e3&amp;&amp;network4h,0=267aedde-187a-4174-b738-10aefdfcbc8f&amp;&amp;network3h\" id=\"16\" title=\"\" backImgSrc=\"http://localhost:8080/static/images/topology/baseMode3.png\" top=\"246\" left=\"272\" zIndex=\"16\" width=\"50\" height=\"50\"/><line strokeweight=\"2\" strokecolor=\"black\" brokenType=\"1\" stroke=\"purple\" path=\"M 324 272 L 652 352\" d=\"M 324 272 L 652 352\" id=\"line14\" style=\"cursor: pointer; fill: none; stroke: black; stroke-width: 2;\" marker-end=\"url(#arrow)\" xBaseMode=\"module16\" xIndex=\"5\" wBaseMode=\"module20\" wIndex=\"7\" attr_prop_attri1=\"2\" attr_prop_attri2=\"3\" attr_prop_attri3=\"4\" attr_prop_attri4=\"5\"/><line strokeweight=\"2\" strokecolor=\"black\" brokenType=\"1\" stroke=\"purple\" path=\"M 298 298 L 534 112\" d=\"M 298 298 L 534 112\" id=\"line12\" style=\"cursor: pointer; fill: none; stroke: black; stroke-width: 2;\" marker-end=\"url(#arrow)\" xBaseMode=\"module16\" xIndex=\"7\" wBaseMode=\"module18\" wIndex=\"4\" attr_prop_attri1=\"2\" attr_prop_attri2=\"3\" attr_prop_attri3=\"4\" attr_prop_attri4=\"5\"/></modes>";
        InputStream is = new ByteArrayInputStream(str1.getBytes("gbk"));
//        File file = new File("C:\\Users\\CQH\\Desktop\\testXml2.xml");
//        FileInputStream in = new FileInputStream(file);

        Reader read = new InputStreamReader(is,"gbk");
        SAXReader reader = new SAXReader();
        Document document = reader.read(read);
        Element root = document.getRootElement();
        List<Element> childElements = root.elements();

        for (Element child : childElements) {
            System.out.println("child:" + child.getName());

            //未知属性名情况下
            List<Attribute> attributeList = child.attributes();
            for (Attribute attr : attributeList) {
                System.out.println(attr.getName() + ": " + attr.getValue());
            }

            //已知属性名情况下
//     System.out.println("id: " + child.attributeValue("id"));

            //未知子元素名情况下
            List<Element> elementList = child.elements();
            for (Element ele : elementList) {
                System.out.println(ele.getName() + ": " + ele.getText());
            }
            System.out.println();

            //已知子元素名的情况下
//      System.out.println("title" + child.elementText("title"));
//      System.out.println("author" + child.elementText("author"));
            //这行是为了格式化美观而存在
            System.out.println();
        }
//        xml2List("abc");
        //解析结果
//        classname: module
//        class: module
//        attr_prop_attri1: a
//        attr_prop_attri2: b
//        attr_prop_attri3: c
//        attr_prop_attri4: d
//        id: 12
//        title:
//        backImgSrc: http://localhost:8080/static/images/topology/switch-istep.png
//        top: 170
//        left: 79
//        zIndex: 12
//        width: 50
//        height: 50
//
//
//        strokeweight: 2
//        strokecolor: black
//        brokenType: 1
//        stroke: purple
//        path: M 131 196 L 559 101
//        d: M 131 196 L 559 101
//        id: line12
//        style: cursor: pointer; fill: none; stroke: black; stroke-width: 2;
//        marker-end: url(#arrow)
//        wBaseMode: module14
//        wIndex: 5
//        xBaseMode: module12
//        xIndex: 5
//        attr_prop_attri1: 1
//        attr_prop_attri2: 2
//        attr_prop_attri3: 3
//        attr_prop_attri4: 4
    }
    private static List<Map<String,String >> xml2List (String xml) throws Exception {
        List<Map<String,String >> list=new ArrayList<>();

        String str1="<?xml version='1.0' encoding='utf8'?><modes><mode classname=\"module\" class=\"module\" attr_prop_attri1=\"a\" attr_prop_attri2=\"b\" attr_prop_attri3=\"c\" attr_prop_attri4=\"d\" id=\"14\" title=\"\" backImgSrc=\"http://localhost:8080/static/images/topology/switch-istep.png\" top=\"75\" left=\"507\" zIndex=\"14\" width=\"50\" height=\"50\"/><mode classname=\"module\" class=\"module\" attr_prop_attri1=\"a\" attr_prop_attri2=\"b\" attr_prop_attri3=\"c\" attr_prop_attri4=\"d\" id=\"12\" title=\"\" backImgSrc=\"http://localhost:8080/static/images/topology/switch-istep.png\" top=\"170\" left=\"79\" zIndex=\"12\" width=\"50\" height=\"50\"/><line strokeweight=\"2\" strokecolor=\"black\" brokenType=\"1\" stroke=\"purple\" path=\"M 131 196 L 559 101\" d=\"M 131 196 L 559 101\" id=\"line12\" style=\"cursor: pointer; fill: none; stroke: black; stroke-width: 2;\" marker-end=\"url(#arrow)\" wBaseMode=\"module14\" wIndex=\"5\" xBaseMode=\"module12\" xIndex=\"5\" attr_prop_attri1=\"1\" attr_prop_attri2=\"2\" attr_prop_attri3=\"3\" attr_prop_attri4=\"4\"/></modes>";

        InputStream is = new ByteArrayInputStream(str1.getBytes("gbk"));
        Reader read = new InputStreamReader(is,"gbk");
        SAXReader reader = new SAXReader();
        Document document = reader.read(read);
        Element root = document.getRootElement();
        List<Element> childElements = root.elements();

        for (Element child : childElements) {
            //未知属性名情况下
            List<Attribute> attributeList = child.attributes();
            Map<String,String > map=new HashMap<>();
            map.put("element",child.getName());
            for (Attribute attr : attributeList) {
//                System.out.println("1:"+attr.getName() + ": " + attr.getValue()+child.getName());

                map.put(attr.getName(),attr.getValue());
            }
            list.add(map);

        }
        System.out.println(list.get(0).get("classname"));

        return list;
    }

}
